**************************************************
* This program performs the main empirical tests * 
* on innovation contribution to firm value       * 
* reported in Table 2 and robustness tables      *
**************************************************

	clear all
	global path "C:\cow"
	global out "$path\out"
	use "$path\innotest.dta"
	set more off

	*Convert strings to numerics
	destring gvkey, gen(firm)	
	encode state, gen(lstateFE)
	encode incorp, gen(istateFE)
	encode port, gen(ffFE)

	*Set timeframe 
	drop if fyear < 1996
	
	*Drop if missing industry
	rename sic sic4
	gen sic3 = floor(sic4/10)
	gen sic2 = floor(sic4/100)
	gen sic1 = floor(sic4/1000)
	drop if missing(sic4)
	*Drop utilities
	drop if sic4 >= 4900 & sic4 <= 4999
	*Drop financials
	drop if sic4 >= 6000 & sic4 <= 6999
	*Drop public administration/non-classifiable firms
	drop if sic4 >= 9000 & sic4 <= 9999

	
	*Drop if missing headquarter state
	drop if missing(state)
	*Drop if missing state of incorporation
	drop if missing(incorp)
	*Drop if not incorporated in U.S. state
	drop if inlist(incorp, "AS", "INTL", "PR", "TT", "VI")

	
	
	gen ln_mveq_rd=log(1+mveq_rd)
	gen rdassets = rd_rd/at_rd
	gen rdassetsn = rdn/assets
	gen rdsales = rd/sales
	gen capxassets = capx/assets
	gen roa=oibdp_rd/at_rd
	drop if mveq_rd<0

	
	gen d_tsm=0
	replace d_tsm=1 if missing(tsm)
	gen d_fnpats=0
	replace d_fnpats=1 if missing(fnpats)
	gen d_cites=0
	replace d_cites=1 if missing(patcites)
	
	*Generate variables
	local inno "tcw tsm fnpats patcount patvalue patcites tcw_1 tsm_1 fnpats_1 tcw_2 tsm_2 fnpats_2 tcw_3 tsm_3 fnpats_3 tcw_01 tsm_01 fnpats_01 tcw_012 tsm_012 fnpats_012 "
	foreach v of local inno {
		replace `v'=0 if missing(`v')
		gen ln_`v' = log(1+`v')
	}

	local varlist "tcw tsm fnpats patcount patvalue patcites tcw_1 tsm_1 fnpats_1 tcw_2 tsm_2 fnpats_2 tcw_3 tsm_3 fnpats_3 tcw_01 tsm_01 fnpats_01 tcw_012 tsm_012 fnpats_012 "
	foreach v of local varlist {
		gen `v'assets = `v'/assets
	}

	
	drop if missing(mveq_rd, bm, rdassets, tsmassets, fnpatsassets, patcitesassets )

	winsor2 rdassets , replace cuts(1 99) trim


	local statelist "DE OK MO KS TX NV NJ MD WA"
	gen treatment = 0
	foreach state of local statelist {
			replace treatment = 1 if incorp == "`state'"
	}


	*Create macros for date when law was passed
		local DE_cow = date("01Jul2000","DMY")
		local OK_cow = date("01Nov2001","DMY")
		local MO_cow = date("01Oct2003","DMY")
		local KS_cow = date("01Jan2005","DMY")
		local TX_cow = date("01Jan2006","DMY")
		local NV_cow = date("01Oct2007","DMY")
		local NJ_cow = date("11Mar2011","DMY")
		local MD_cow = date("01Oct2014","DMY")
		local WA_cow = date("01Jan2016","DMY")
		
		* adjust calendar year of fyeendt to be next year if fyenddt month < cow law date
		gen ayear = year(datadate)
		local statelist "DE OK MO KS TX NV NJ MD WA"
		foreach state of local statelist {
				replace ayear = year(datadate)-1 if incorp == "`state'" & month(datadate) < month(``state'_cow')
				}

		*cow: indicator equal to one if firm is incorporated in a state that has passed a COW law
		gen cow = 0
		local statelist "DE OK MO KS TX NV NJ MD WA"
		foreach state of local statelist {
				replace cow = 1 if incorp == "`state'" & ayear >= year(``state'_cow')
				}

				
		gen cow_rdassets=cow*rdassets
		gen cow_tsmassets=cow*tsmassets
		gen cow_fnpatsassets=cow*fnpatsassets
		gen cow_patcitesassets=cow*patcitesassets
		gen cow_tcwassets=cow*tcwassets
		gen cow_patentrd=cow*patentrd
		gen cow_citepatent=cow*citepatent
		gen ln_q=log(q)
		gen ln_totalq=log(totalq)
		gen cow_d_rdmiss=cow*d_rdmiss
		gen ln_at=log(at_rd)


	* save processed data for later use
	save "$path\cow\innotest_processed.dta", replace

	
	
	
*********************************
* Table 2 Panel A Summary stats *
*********************************

		distinct gvkey
		winsor2 tsmassets , replace cuts(1 99) trim
		estpost tabstat cow ln_q rdassets tsmassets fnpatsassets mveq_rd leverage_mkt q roa ceoownership, statistics (n mean sd p25 p50 p75) columns(statistics)
		
		
*******************************
* Table 2 Panel B Regressions *
*******************************

		reghdfe ln_q rdassets cow cow_rdassets d_rd, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)

		reghdfe ln_q rdassets cow cow_rdassets d_rd ln_at leverage_mkt roa, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm ln_at leverage_mkt roa, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats ln_at leverage_mkt roa, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)


		
		
		
		
		
*======================================*
*           ROBUSTNESS TESTS           *
*======================================*
	
		* noncompete index		
		gen nceindex=0
		replace nceindex=5 if incorp=="AL"
		replace nceindex=3 if incorp=="AK"
		replace nceindex=3 if incorp=="AZ"
		replace nceindex=5 if incorp=="AR"
		replace nceindex=0 if incorp=="CA"
		replace nceindex=1 if incorp=="CO"
		replace nceindex=2 if incorp=="CO" & fyear>=1992 
		replace nceindex=3 if incorp=="CO" & fyear>=2012 
		replace nceindex=4 if incorp=="CT"
		replace nceindex=3 if incorp=="CT" & fyear>=1992
		replace nceindex=6 if incorp=="DE"
		replace nceindex=7 if incorp=="DC"
		replace nceindex=7 if incorp=="FL"
		replace nceindex=9 if incorp=="FL" & fyear>=1997
		replace nceindex=5 if incorp=="GA"
		replace nceindex=6 if incorp=="GA" & fyear>=2005
		replace nceindex=7 if incorp=="GA" & fyear>=2011
		replace nceindex=3 if incorp=="HI"
		replace nceindex=4 if incorp=="HI" & fyear>=2007
		replace nceindex=5 if incorp=="ID"
		replace nceindex=6 if incorp=="ID" & fyear>=1992
		replace nceindex=7 if incorp=="ID" & fyear>=2009
		replace nceindex=5 if incorp=="IL"
		replace nceindex=6 if incorp=="IL" & (2012<=fyear<=2013) 
		replace nceindex=5 if incorp=="IN"
		replace nceindex=6 if incorp=="IA"
		replace nceindex=6 if incorp=="KS"
		replace nceindex=7 if incorp=="KS" & fyear>=2008
		replace nceindex=6 if incorp=="KY"
		replace nceindex=5 if incorp=="KY" & fyear>=2015
		replace nceindex=2 if incorp=="LA" & fyear<=1991
		replace nceindex=4 if incorp=="LA" & (1992<=fyear<=2001)
		replace nceindex=4 if incorp=="LA" & (2004<=fyear)
		replace nceindex=4 if incorp=="ME"
		replace nceindex=5 if incorp=="MD"
		replace nceindex=6 if incorp=="MA"
		replace nceindex=5 if incorp=="MI"
		replace nceindex=5 if incorp=="MN"
		replace nceindex=4 if incorp=="MS"
		replace nceindex=5 if incorp=="MS" & fyear>=2009
		replace nceindex=7 if incorp=="MO"
		replace nceindex=2 if incorp=="MT"
		replace nceindex=4 if incorp=="NE"
		replace nceindex=5 if incorp=="NV"
		replace nceindex=2 if incorp=="NH"
		replace nceindex=1 if incorp=="NH" & fyear>=2013
		replace nceindex=4 if incorp=="NJ"
		replace nceindex=2 if incorp=="NM"
		replace nceindex=3 if incorp=="NY"
		replace nceindex=4 if incorp=="NC"
		replace nceindex=0 if incorp=="ND"
		replace nceindex=4 if incorp=="OH"
		replace nceindex=5 if incorp=="OH" & fyear>=1992
		replace nceindex=6 if incorp=="OH" & fyear>=2005
		replace nceindex=1 if incorp=="OK"
		replace nceindex=6 if incorp=="OR"
		replace nceindex=5 if incorp=="OR" & fyear>=2008
		replace nceindex=6 if incorp=="PA"
		replace nceindex=3 if incorp=="RI"
		replace nceindex=5 if incorp=="SC"
		replace nceindex=4 if incorp=="SC" & fyear>=2011
		replace nceindex=5 if incorp=="SD"
		replace nceindex=7 if incorp=="TN"
		replace nceindex=5 if incorp=="TX"
		replace nceindex=3 if incorp=="TX" & fyear>=1995
		replace nceindex=4 if incorp=="TX" & fyear>=2011
		replace nceindex=6 if incorp=="UT"
		replace nceindex=5 if incorp=="VT"
		replace nceindex=6 if incorp=="VT" & fyear>=2006
		replace nceindex=4 if incorp=="VA" & (1980<=fyear<=1991)
		replace nceindex=3 if incorp=="VA" & (1992<=fyear<=2005)
		replace nceindex=4 if incorp=="VA" & (2006<=fyear<=2013)
		replace nceindex=5 if incorp=="VA" & (2014<=fyear)
		replace nceindex=5 if incorp=="WA"
		replace nceindex=2 if incorp=="WV" 
		replace nceindex=3 if incorp=="WV" & (1984<=fyear<=1991)
		replace nceindex=3 if incorp=="WI"
		replace nceindex=4 if incorp=="WI" & fyear>=2010
		replace nceindex=5 if incorp=="WI" & fyear>=2016
		replace nceindex=4 if incorp=="WY"

		* universal demand law
		gen ud=0
		replace ud=1 if incorp=="UT" & fyear>=1992
		replace ud=1 if incorp=="NH" & fyear>=1993
		replace ud=1 if incorp=="MS" & fyear>=1993
		replace ud=1 if incorp=="NC" & fyear>=1995
		replace ud=1 if incorp=="AZ" & fyear>=1996
		replace ud=1 if incorp=="NE" & fyear>=1996
		replace ud=1 if incorp=="CT" & fyear>=1997
		replace ud=1 if incorp=="ME" & fyear>=1997
		replace ud=1 if incorp=="PA" & fyear>=1997
		replace ud=1 if incorp=="TX" & fyear>=1997
		replace ud=1 if incorp=="WY" & fyear>=1997
		replace ud=1 if incorp=="ID" & fyear>=1998
		replace ud=1 if incorp=="HI" & fyear>=2001
		replace ud=1 if incorp=="IA" & fyear>=2003
		replace ud=1 if incorp=="MA" & fyear>=2004
		replace ud=1 if incorp=="RI" & fyear>=2005
		replace ud=1 if incorp=="SD" & fyear>=2005
		
		* The five most common types of antitakeover laws adopted by states since 1982. The law types are: control share acquisition laws (CS), business combination laws (BC), fair price laws (FP), directors' duties laws (DD), and poison pill laws (PP)
 
		* cs law
		gen cs=0
		replace cs =1 if incorp=="AZ" & fyear>=1987
		replace cs =1 if incorp=="FL" & fyear>=1987
		replace cs =1 if incorp=="HI" & fyear>=1985
		replace cs =1 if incorp=="ID" & fyear>=1988
		replace cs =1 if incorp=="IN" & fyear>=1986
		replace cs =1 if incorp=="KS" & fyear>=1988
		replace cs =1 if incorp=="LA" & fyear>=1987
		replace cs =1 if incorp=="MD" & fyear>=1989
		replace cs =1 if incorp=="MA" & fyear>=1987
		replace cs =1 if incorp=="MI" & fyear>=1988
		replace cs =1 if incorp=="MN" & fyear>=1984
		replace cs =1 if incorp=="MS" & fyear>=1990
		replace cs =1 if incorp=="MO" & fyear>=1984
		replace cs =1 if incorp=="NE" & fyear>=1988
		replace cs =1 if incorp=="NV" & fyear>=1987
		replace cs =1 if incorp=="NC" & fyear>=1987
		replace cs =1 if incorp=="OH" & fyear>=1982
		replace cs =1 if incorp=="OK" & fyear>=1987
		replace cs =1 if incorp=="OR" & fyear>=1987
		replace cs =1 if incorp=="PA" & fyear>=1990
		replace cs =1 if incorp=="SC" & fyear>=1988
		replace cs =1 if incorp=="SD" & fyear>=1990
		replace cs =1 if incorp=="TN" & fyear>=1988
		replace cs =1 if incorp=="UT" & fyear>=1987
		replace cs =1 if incorp=="VA" & fyear>=1989
		replace cs =1 if incorp=="WI" & fyear>=1984
		replace cs =1 if incorp=="WY" & fyear>=1990

		* bc law
		gen bc=0
		replace bc =1 if incorp=="AZ" & fyear>=1987
		replace bc =1 if incorp=="CT" & fyear>=1988
		replace bc =1 if incorp=="DE" & fyear>=1988
		replace bc =1 if incorp=="GA" & fyear>=1988
		replace bc =1 if incorp=="ID" & fyear>=1988
		replace bc =1 if incorp=="IL" & fyear>=1989
		replace bc =1 if incorp=="IN" & fyear>=1986
		replace bc =1 if incorp=="IA" & fyear>=1997
		replace bc =1 if incorp=="KS" & fyear>=1989
		replace bc =1 if incorp=="KY" & fyear>=1986
		replace bc =1 if incorp=="ME" & fyear>=1988
		replace bc =1 if incorp=="MD" & fyear>=1989
		replace bc =1 if incorp=="MA" & fyear>=1989
		replace bc =1 if incorp=="MI" & fyear>=1989
		replace bc =1 if incorp=="MN" & fyear>=1987
		replace bc =1 if incorp=="MO" & fyear>=1986
		replace bc =1 if incorp=="NE" & fyear>=1988
		replace bc =1 if incorp=="NV" & fyear>=1991
		replace bc =1 if incorp=="NJ" & fyear>=1986
		replace bc =1 if incorp=="NY" & fyear>=1985
		replace bc =1 if incorp=="OH" & fyear>=1990
		replace bc =1 if incorp=="OK" & fyear>=1991
		replace bc =1 if incorp=="OR" & fyear>=1991
		replace bc =1 if incorp=="PA" & fyear>=1988
		replace bc =1 if incorp=="RI" & fyear>=1990
		replace bc =1 if incorp=="SC" & fyear>=1988
		replace bc =1 if incorp=="SD" & fyear>=1990
		replace bc =1 if incorp=="TN" & fyear>=1988
		replace bc =1 if incorp=="TX" & fyear>=1997
		replace bc =1 if incorp=="VA" & fyear>=1988
		replace bc =1 if incorp=="WA" & fyear>=1987
		replace bc =1 if incorp=="WI" & fyear>=1987
		replace bc =1 if incorp=="WY" & fyear>=1989

		* fp law
		gen fp=0
		replace fp =1 if incorp=="AZ" & fyear>=1987
		replace fp =1 if incorp=="CT" & fyear>=1984
		replace fp =1 if incorp=="FL" & fyear>=1987
		replace fp =1 if incorp=="GA" & fyear>=1985
		replace fp =1 if incorp=="ID" & fyear>=1988
		replace fp =1 if incorp=="IL" & fyear>=1985
		replace fp =1 if incorp=="IN" & fyear>=1986
		replace fp =1 if incorp=="KY" & fyear>=1984
		replace fp =1 if incorp=="LA" & fyear>=1984
		replace fp =1 if incorp=="MD" & fyear>=1983
		replace fp =1 if incorp=="MI" & fyear>=1984
		replace fp =1 if incorp=="MN" & fyear>=1991
		replace fp =1 if incorp=="MS" & fyear>=1985
		replace fp =1 if incorp=="MO" & fyear>=1986
		replace fp =1 if incorp=="NV" & fyear>=1991
		replace fp =1 if incorp=="NJ" & fyear>=1986
		replace fp =1 if incorp=="NY" & fyear>=1985
		replace fp =1 if incorp=="NC" & fyear>=1987
		replace fp =1 if incorp=="OH" & fyear>=1990
		replace fp =1 if incorp=="PA" & fyear>=1988
		replace fp =1 if incorp=="RI" & fyear>=1990
		replace fp =1 if incorp=="SC" & fyear>=1988
		replace fp =1 if incorp=="SD" & fyear>=1990
		replace fp =1 if incorp=="TN" & fyear>=1988
		replace fp =1 if incorp=="VA" & fyear>=1985
		replace fp =1 if incorp=="WA" & fyear>=1985
		replace fp =1 if incorp=="WI" & fyear>=1984

		* dd law
		gen dd=0
		replace dd =1 if incorp=="AZ" & fyear>=1987
		replace dd =1 if incorp=="CT" & fyear>=1988
		replace dd =1 if incorp=="FL" & fyear>=1989
		replace dd =1 if incorp=="GA" & fyear>=1989
		replace dd =1 if incorp=="HI" & fyear>=1989
		replace dd =1 if incorp=="ID" & fyear>=1988
		replace dd =1 if incorp=="IL" & fyear>=1985
		replace dd =1 if incorp=="IN" & fyear>=1986
		replace dd =1 if incorp=="IA" & fyear>=1989
		replace dd =1 if incorp=="KY" & fyear>=1988
		replace dd =1 if incorp=="LA" & fyear>=1988
		replace dd =1 if incorp=="ME" & fyear>=1985
		replace dd =1 if incorp=="MD" & fyear>=1999
		replace dd =1 if incorp=="MA" & fyear>=1989
		replace dd =1 if incorp=="MN" & fyear>=1987
		replace dd =1 if incorp=="MS" & fyear>=1990
		replace dd =1 if incorp=="MO" & fyear>=1986
		replace dd =1 if incorp=="NE" & fyear>=1988
		replace dd =1 if incorp=="NV" & fyear>=1991
		replace dd =1 if incorp=="NJ" & fyear>=1989
		replace dd =1 if incorp=="NM" & fyear>=1987
		replace dd =1 if incorp=="NY" & fyear>=1987
		replace dd =1 if incorp=="NC" & fyear>=1993
		replace dd =1 if incorp=="ND" & fyear>=1993
		replace dd =1 if incorp=="OH" & fyear>=1984
		replace dd =1 if incorp=="OR" & fyear>=1989
		replace dd =1 if incorp=="PA" & fyear>=1990
		replace dd =1 if incorp=="RI" & fyear>=1990
		replace dd =1 if incorp=="SD" & fyear>=1990
		replace dd =1 if incorp=="TN" & fyear>=1988
		replace dd =1 if incorp=="TX" & fyear>=2003
		replace dd =1 if incorp=="VT" & fyear>=1998
		replace dd =1 if incorp=="VA" & fyear>=1988
		replace dd =1 if incorp=="WI" & fyear>=1987
		replace dd =1 if incorp=="WY" & fyear>=1990

		* pp law
		gen pp=0
		replace pp =1 if incorp=="CO" & fyear>=1989
		replace pp =1 if incorp=="CT" & fyear>=2003
		replace pp =1 if incorp=="FL" & fyear>=1989
		replace pp =1 if incorp=="GA" & fyear>=1988
		replace pp =1 if incorp=="HI" & fyear>=1988
		replace pp =1 if incorp=="ID" & fyear>=1988
		replace pp =1 if incorp=="IL" & fyear>=1989
		replace pp =1 if incorp=="IN" & fyear>=1986
		replace pp =1 if incorp=="IA" & fyear>=1989
		replace pp =1 if incorp=="KY" & fyear>=1988
		replace pp =1 if incorp=="ME" & fyear>=2002
		replace pp =1 if incorp=="MD" & fyear>=1999
		replace pp =1 if incorp=="MA" & fyear>=1989
		replace pp =1 if incorp=="MI" & fyear>=2001
		replace pp =1 if incorp=="MN" & fyear>=1995
		replace pp =1 if incorp=="MS" & fyear>=2005
		replace pp =1 if incorp=="MO" & fyear>=1999
		replace pp =1 if incorp=="NV" & fyear>=1989
		replace pp =1 if incorp=="NJ" & fyear>=1989
		replace pp =1 if incorp=="NY" & fyear>=1988
		replace pp =1 if incorp=="NC" & fyear>=1989
		replace pp =1 if incorp=="OH" & fyear>=1986
		replace pp =1 if incorp=="OR" & fyear>=1989
		replace pp =1 if incorp=="PA" & fyear>=1988
		replace pp =1 if incorp=="RI" & fyear>=1990
		replace pp =1 if incorp=="SC" & fyear>=1998
		replace pp =1 if incorp=="SD" & fyear>=1990
		replace pp =1 if incorp=="TN" & fyear>=1989
		replace pp =1 if incorp=="TX" & fyear>=2003
		replace pp =1 if incorp=="UT" & fyear>=1989
		replace pp =1 if incorp=="VT" & fyear>=2008
		replace pp =1 if incorp=="VA" & fyear>=1990
		replace pp =1 if incorp=="WA" & fyear>=1998
		replace pp =1 if incorp=="WI" & fyear>=1987
		replace pp =1 if incorp=="WY" & fyear>=2009

		* Revlon case
		gen revlon=0
		replace revlon =1 if incorp=="CA" & fyear>=2002
		replace revlon =1 if incorp=="DE" & fyear>=1986
		replace revlon =1 if incorp=="IL" & fyear>=1988
		replace revlon =1 if incorp=="IN" & fyear>=1987
		replace revlon =-1 if incorp=="IN" & fyear>=2001
		replace revlon =1 if incorp=="KS" & fyear>=2003
		replace revlon =-1 if incorp=="MD" & fyear>=2004
		replace revlon =1 if incorp=="MD" & fyear>=2009
		replace revlon =1 if incorp=="MI" & fyear>=1986
		replace revlon =1 if incorp=="MN" & fyear>=1987
		replace revlon =1 if incorp=="MO" & fyear>=1999
		replace revlon =1 if incorp=="NV" & fyear>=1997
		replace revlon =-1 if incorp=="NV" & fyear>=1999
		replace revlon =1 if incorp=="NH" & fyear>=2001
		replace revlon =1 if incorp=="NY" & fyear>=1986
		replace revlon =-1 if incorp=="NY" & fyear>=1997
		replace revlon =-1 if incorp=="NC" & fyear>=2001
		replace revlon =-1 if incorp=="OH" & fyear>=1990
		replace revlon =-1 if incorp=="PA" & fyear>=1987
		replace revlon =-1 if incorp=="VA" & fyear>=1999
		replace revlon =-1 if incorp=="WI" & fyear>=1999
		
		* Unocal case
		gen unocal=0
		replace unocal =1 if incorp=="AR" & fyear>=1993
		replace unocal =1 if incorp=="CA" & fyear>=1989
		replace unocal =1 if incorp=="DE" & fyear>=1985
		replace unocal =1 if incorp=="CA" & fyear>=2002
		replace unocal =1 if incorp=="FL" & fyear>=1989
		replace unocal =1 if incorp=="IL" & fyear>=1988
		replace unocal =1 if incorp=="IN" & fyear>=1987
		replace unocal =-1 if incorp=="IN" & fyear>=2001
		replace unocal =1 if incorp=="KS" & fyear>=2003
		replace unocal =-1 if incorp=="MD" & fyear>=1988
		replace unocal =1 if incorp=="MD" & fyear>=2004
		replace unocal =-1 if incorp=="MA" & fyear>=2003
		replace unocal =1 if incorp=="MI" & fyear>=1986
		replace unocal =1 if incorp=="MN" & fyear>=1987
		replace unocal =1 if incorp=="MO" & fyear>=1999
		replace unocal =-1 if incorp=="NV" & fyear>=1985
		replace unocal =-1 if incorp=="NJ" & fyear>=1998
		replace unocal =-1 if incorp=="NY" & fyear>=1986
		replace unocal =-1 if incorp=="NC" & fyear>=2001
		replace unocal =-1 if incorp=="OH" & fyear>=1986
		replace unocal =-1 if incorp=="PA" & fyear>=1986
		replace unocal =1 if incorp=="TX" & fyear>=1989
		replace unocal =-1 if incorp=="VA" & fyear>=1995
		replace unocal =1 if incorp=="WI" & fyear>=1989

		* Blasius case
		gen blasius=0
		replace blasius =1 if incorp=="CT" & fyear>=2000
		replace blasius =1 if incorp=="DE" & fyear>=1988
		replace blasius =-1 if incorp=="GA" & fyear>=1997
		replace blasius =-1 if incorp=="IN" & fyear>=1993
		replace blasius =-1 if incorp=="IN" & fyear>=2001
		replace blasius =1 if incorp=="KS" & fyear>=2003
		replace blasius =-1 if incorp=="MD" & fyear>=1999
		replace blasius =1 if incorp=="MD" & fyear>=2005
		replace blasius =1 if incorp=="MA" & fyear>=1990
		replace blasius =1 if incorp=="MI" & fyear>=2003
		replace blasius =1 if incorp=="NV" & fyear>=1997
		replace blasius =-1 if incorp=="NV" & fyear>=1999
		replace blasius =-1 if incorp=="NJ" & fyear>=1998
		replace blasius =-1 if incorp=="NC" & fyear>=1984
		replace blasius =-1 if incorp=="OH" & fyear>=1986
		replace blasius =-1 if incorp=="PA" & fyear>=1990
		replace blasius =-1 if incorp=="VA" & fyear>=1986
		
		* Madatory classified board law
		gen mcb=0
		replace mcb =1 if incorp=="IN" & fyear>=2009
		replace mcb =1 if incorp=="IA" & fyear>=2011
		replace mcb =-1 if incorp=="IA" & fyear>=2014
		replace mcb =1 if incorp=="MA" & fyear>=1990
		replace mcb =1 if incorp=="OK" & fyear>=2010
		replace mcb =-1 if incorp=="OK" & fyear>=2013
		
		
********************************
* Table 7 Managerial ownership *
********************************

		reghdfe ln_q c.rdassets##c.ceoownership cow##c.ceoownership c.cow_rdassets##c.ceoownership d_rd , absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q c.tsmassets##c.ceoownership cow##c.ceoownership c.cow_tsmassets##c.ceoownership d_tsm , absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q c.fnpatsassets##c.ceoownership cow##c.ceoownership c.cow_fnpatsassets##c.ceoownership d_fnpats , absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)

		
*********************************
* Table 8 Independent directors *
*********************************

		reghdfe ln_q c.rdassets##c.pct_inddir cow##c.pct_inddir c.cow_rdassets##c.pct_inddir d_rd, absorb(firm lstate#fyear sic4#fyear) vce(cluster istate)
		reghdfe ln_q c.tsmassets##c.pct_inddir cow##c.pct_inddir c.cow_tsmassets##c.pct_inddir d_tsm , absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q c.fnpatsassets##c.pct_inddir cow##c.pct_inddir c.cow_fnpatsassets##c.pct_inddir d_fnpats , absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		
			
************************************	
* Table 10 HQ state = Incorp state *
************************************	

		reghdfe ln_q rdassets cow cow_rdassets d_rd ln_at   if incorp==state, absorb(firm lstate#sic1#fyear) 
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm  if incorp==state, absorb(firm lstate#sic2#fyear) 
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats  if incorp==state, absorb(firm lstate#sic2#fyear) 

		
**************************	
* Table 11 Drop DE firms *
**************************	
		
		reghdfe ln_q rdassets cow cow_rdassets d_rd ln_at leverage_mkt roa if state~="DE", absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm ln_at leverage_mkt roa if state~="DE", absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats ln_at leverage_mkt roa if state~="DE", absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)

		
*************************************	
* Table 12 Additional firm controls *
*************************************

		local control "ceoownership div busseg activeownership5 activeownership "
		foreach v of local control {
		replace `v'=0 if missing(`v') 
		}
	
		reghdfe ln_q rdassets cow cow_rdassets d_rd ln_at leverage_mkt roa cbdefml ceoownership activeownership5 div busseg cown_mean, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm ln_at leverage_mkt roa cbdefml ceoownership activeownership5 div busseg cown_mean, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats ln_at leverage_mkt roa cbdefml ceoownership activeownership5 div busseg cown_mean, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)

		
**************************************	
* Table 13 Additional legal controls *
**************************************

		reghdfe ln_q rdassets cow cow_rdassets d_rd nceindex ud mcb cs bc fp dd pp revlon unocal blasius, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm nceindex ud mcb cs bc fp dd pp revlon unocal blasius, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats nceindex ud mcb cs bc fp dd pp revlon unocal blasius, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)

		
*******************************************	
* Table 14 High dimensional fixed effects *
*******************************************	

		reghdfe ln_q rdassets cow cow_rdassets d_rd, absorb(firm lstate#sic2#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm, absorb(firm lstate#sic2#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats, absorb(firm lstate#sic2#fyear) vce(cluster istate)
		
		reghdfe ln_q rdassets cow cow_rdassets d_rd ln_at leverage_mkt roa, absorb(firm lstate#sic2#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm ln_at leverage_mkt roa, absorb(firm lstate#sic2#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats ln_at leverage_mkt roa, absorb(firm lstate#sic2#fyear) vce(cluster istate)
			
		
************************	
* Table 15 Matched DiD *
************************
				
		local control "ceoownership div busseg activeownership5 activeownership cown_mean "
		foreach v of local control {
		replace `v'=0 if missing(`v') 
		}
		
	
		psmatch2 treatment ln_at leverage_mkt roa cbdefml ceoownership activeownership5 div busseg cown_mean i.lstateFE i.fyear i.sic2, n(1)		
		bys permno: egen matching_sample = sum(_weight)
		reghdfe ln_q rdassets cow cow_rdassets d_rd if matching_sample!=0, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm if matching_sample!=0, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats if matching_sample!=0, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)

		
************************		
* Table 16 Stacked DiD *
************************		

		expand 2, gen(event_DE)
		drop if event_DE==1 & treatment==1 & incorp~="DE" 
		drop if event_DE==1 & (ayear<1996 | ayear>2004)

		expand 2 if event_DE==0, gen(event_OK)
		drop if event_OK==1 & treatment==1 & incorp~="OK"
		drop if event_OK==1 & (ayear<1997 | ayear>2005)

		expand 2 if event_DE==0 & event_OK==0, gen(event_MO)
		drop if event_MO==1 & treatment==1 & incorp~="MO"
		drop if event_MO==1 & (ayear<1999 | ayear>2007)
		
		expand 2 if event_DE==0 & event_OK==0 & event_MO==0, gen(event_KS)
		drop if event_KS==1 & treatment==1 & incorp~="KS"
		drop if event_KS==1 & (ayear<2001 | ayear>2009)
		
		expand 2 if event_DE==0 & event_OK==0 & event_MO==0 & event_KS==0, gen(event_TX)
		drop if event_TX==1 & treatment==1 & incorp~="TX"
		drop if event_TX==1 & (ayear<2002 | ayear>2010)
		
		expand 2 if event_DE==0 & event_OK==0 & event_MO==0 & event_KS==0 & event_TX==0, gen(event_NV)
		drop if event_NV==1 & treatment==1 & incorp~="NV"
		drop if event_NV==1 & (ayear<2003 | ayear>2011)
		
		expand 2 if event_DE==0 & event_OK==0 & event_MO==0 & event_KS==0 & event_TX==0 & event_NV==0, gen(event_NJ)
		drop if event_NJ==1 & treatment==1 & incorp~="NJ"
		drop if event_NJ==1 & (ayear<2007 | ayear>2015)
		
		expand 2 if event_DE==0 & event_OK==0 & event_MO==0 & event_KS==0 & event_TX==0 & event_NV==0 & event_NJ==0, gen(event_MD)
		drop if event_MD==1 & treatment==1 & incorp~="MD"
		drop if event_MD==1 & (ayear<2010 | ayear>2018) 
		
		expand 2 if event_DE==0 & event_OK==0 & event_MO==0 & event_KS==0 & event_TX==0 & event_NV==0 & event_NJ==0 & event_MD==0, gen(event_WA)
		drop if event_WA==1 & treatment==1 & incorp~="WA"
		drop if event_WA==1 & (ayear<2012 | ayear>2020)
		
		drop if event_DE==0 & event_OK==0 & event_MO==0 & event_KS==0 & event_TX==0 & event_NV==0 & event_NJ==0 & event_MD==0 & event_WA==0
		
		reghdfe ln_q rdassets cow cow_rdassets d_rd, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)

		reghdfe ln_q rdassets cow cow_rdassets d_rd ln_at leverage_mkt roa, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cow cow_tsmassets d_tsm ln_at leverage_mkt roa, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cow cow_fnpatsassets d_fnpats ln_at leverage_mkt roa, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
				
				
********************************
* Table B3 Dynamic coeff trend *
********************************
		
	clear all
	global path "C:\cow"
	global out "$path\out"
	use "$path\innotest.dta"
	set more off

	*Convert strings to numerics
	destring gvkey, gen(firm)	
	encode state, gen(lstateFE)
	encode incorp, gen(istateFE)
	encode port, gen(ffFE)

	*Set timeframe 
	drop if fyear < 1996
	
	*Drop if missing industry
	rename sic sic4
	gen sic3 = floor(sic4/10)
	gen sic2 = floor(sic4/100)
	gen sic1 = floor(sic4/1000)
	drop if missing(sic4)
	*Drop utilities
	drop if sic4 >= 4900 & sic4 <= 4999
	*Drop financials
	drop if sic4 >= 6000 & sic4 <= 6999
	*Drop public administration/non-classifiable firms
	drop if sic4 >= 9000 & sic4 <= 9999

	
	*Drop if missing headquarter state
	drop if missing(state)
	*Drop if missing state of incorporation
	drop if missing(incorp)
	*Drop if not incorporated in U.S. state
	drop if inlist(incorp, "AS", "INTL", "PR", "TT", "VI")

	
	
	gen ln_mveq_rd=log(1+mveq_rd)
	gen rdassets = rd_rd/at_rd
	gen rdassetsn = rdn/assets
	gen rdsales = rd/sales
	gen capxassets = capx/assets
	gen roa=oibdp_rd/at_rd
	drop if mveq_rd<0

	
	gen d_tsm=0
	replace d_tsm=1 if missing(tsm)
	gen d_fnpats=0
	replace d_fnpats=1 if missing(fnpats)
	gen d_cites=0
	replace d_cites=1 if missing(patcites)
	
	*Generate variables
	local inno "tcw tsm fnpats patcount patvalue patcites tcw_1 tsm_1 fnpats_1 tcw_2 tsm_2 fnpats_2 tcw_3 tsm_3 fnpats_3 tcw_01 tsm_01 fnpats_01 tcw_012 tsm_012 fnpats_012 "
	foreach v of local inno {
		replace `v'=0 if missing(`v')
		gen ln_`v' = log(1+`v')
	}

	local varlist "tcw tsm fnpats patcount patvalue patcites tcw_1 tsm_1 fnpats_1 tcw_2 tsm_2 fnpats_2 tcw_3 tsm_3 fnpats_3 tcw_01 tsm_01 fnpats_01 tcw_012 tsm_012 fnpats_012 "
	foreach v of local varlist {
		gen `v'assets = `v'/assets
	}

	
	drop if missing(mveq_rd, bm, rdassets, tsmassets, fnpatsassets, patcitesassets )

	winsor2 rdassets , replace cuts(1 99) trim


	local statelist "DE OK MO KS TX NV NJ MD WA"
	gen treatment = 0
	foreach state of local statelist {
			replace treatment = 1 if incorp == "`state'"
	}


	*Create macros for date when law was passed
		local DE_cow = date("01Jul2000","DMY")
		local OK_cow = date("01Nov2001","DMY")
		local MO_cow = date("01Oct2003","DMY")
		local KS_cow = date("01Jan2005","DMY")
		local TX_cow = date("01Jan2006","DMY")
		local NV_cow = date("01Oct2007","DMY")
		local NJ_cow = date("11Mar2011","DMY")
		local MD_cow = date("01Oct2014","DMY")
		local WA_cow = date("01Jan2016","DMY")
		
		* adjust calendar year of fyeendt to be next year if fyenddt month < cow law date
		gen ayear = year(datadate)
		local statelist "DE OK MO KS TX NV NJ MD WA"
		foreach state of local statelist {
				replace ayear = year(datadate)-1 if incorp == "`state'" & month(datadate) < month(``state'_cow')
				}

		*cow: indicator equal to one if firm is incorporated in a state that has passed a COW law
		gen cow = 0
		local statelist "DE OK MO KS TX NV NJ MD WA"
		foreach state of local statelist {
				replace cow = 1 if incorp == "`state'" & ayear >= year(``state'_cow')
				}

				
		gen cow_rdassets=cow*rdassets
		gen cow_tsmassets=cow*tsmassets
		gen cow_fnpatsassets=cow*fnpatsassets
		gen cow_patcitesassets=cow*patcitesassets
		gen cow_tcwassets=cow*tcwassets
		gen cow_patentrd=cow*patentrd
		gen cow_citepatent=cow*citepatent
		gen ln_q=log(q)
		gen ln_totalq=log(totalq)
		gen cow_d_rdmiss=cow*d_rdmiss
		gen ln_at=log(at_rd)

		
		*Initialize variables
		forval x=1/25 {
			gen cown`x'=0
			}

		forval x=0/25 {
			gen cow`x'=0
			}


		gen cow2p = 0
		gen cow3p = 0
		gen cow4p = 0

		*Generate event time index
		gen indexyear=0
		foreach state of local statelist {
			forval x=-25(1)25 {
			replace indexyear=`x' if incorp == "`state'" & ayear == year(``state'_cow')+`x'
			}
			}	
		
		*Generate event time dummies
		foreach state of local statelist {
			forval x=1/25 {
			replace cown`x' = 1 if incorp == "`state'" & ayear == year(``state'_cow')-`x'
			}
			}	
			
		foreach state of local statelist {
			forval x=0/25 {
			replace cow`x' = 1 if incorp == "`state'" & ayear == year(``state'_cow')+`x'
			}
			}
			
		foreach state of local statelist {
			replace cow2p = 1 if incorp == "`state'" & ayear >= year(``state'_cow')+2
			replace cow3p = 1 if incorp == "`state'" & ayear >= year(``state'_cow')+3
			replace cow4p = 1 if incorp == "`state'" & ayear >= year(``state'_cow')+4
			}
			


		reghdfe ln_q rdassets cown2 cown1 cow0 cow1 cow2 cow3p cown2_rdassets cown1_rdassets cow0_rdassets cow1_rdassets cow2_rdassets cow3p_rdassets d_rd, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q tsmassets cown2 cown1 cow0 cow1 cow2 cow3p cown2_tsmassets cown1_tsmassets cow0_tsmassets cow1_tsmassets cow2_tsmassets cow3p_tsmassets d_tsm, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		reghdfe ln_q fnpatsassets cown2 cown1 cow0 cow1 cow2 cow3p cown2_fnpatsassets cown1_fnpatsassets cow0_fnpatsassets cow1_fnpatsassets cow2_fnpatsassets cow3p_fnpatsassets d_fnpats, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		

*******************************
* Figure B2 Block permutation *
*******************************
	
	clear all
	global path "C:\cow"
	global out "$path\out"
	use "$path\innotest.dta"
	set more off

	*Convert strings to numerics
	destring gvkey, gen(firm)	
	encode state, gen(lstateFE)
	encode incorp, gen(istateFE)
	encode port, gen(ffFE)

	*Set timeframe 
	drop if fyear < 1996 

	*Drop if missing industry
	rename sic sic4
	gen sic3 = floor(sic4/10)
	gen sic2 = floor(sic4/100)
	gen sic1 = floor(sic4/1000)
	drop if missing(sic4)
	*Drop utilities
	drop if sic4 >= 4900 & sic4 <= 4999
	*Drop financials
	drop if sic4 >= 6000 & sic4 <= 6999
	*Drop public administration/non-classifiable firms
	drop if sic4 >= 9000 & sic4 <= 9990


	*Drop if missing headquarter state
	drop if missing(state)
	*Drop if missing state of incorporation
	drop if missing(incorp)
	*Drop if not incorporated in U.S. state
	drop if inlist(incorp, "AS", "INTL", "PR", "TT", "VI")

	gen ln_mveq_rd=log(1+mveq_rd)
	gen rdassets = rd_rd/at_rd
	gen rdsales = rd/sales
	gen capxassets = capx/assets
	gen roa=oibdp_rd/at_rd
	drop if mveq_rd<0

	gen d_tsm=0
	replace d_tsm=1 if missing(tsm)
	gen d_fnpats=0
	replace d_fnpats=1 if missing(fnpats)
	
	*Generate variables
	local inno "tcw tsm fnpats patcites "
	foreach v of local inno {
		replace `v'=0 if missing(`v')
		gen ln_`v' = log(1+`v')
	}
	local varlist "tcw tsm fnpats patcites "
	foreach v of local varlist {
		gen `v'assets = `v'/assets
	}

	drop if missing(mveq_rd, bm, rdassets, tsmassets, fnpatsassets, patcitesassets )
	winsor2 rdassets , replace cuts(1 99) trim
	gen ln_q=log(q)
	

	* Loop 1000;

		forval y= 1/1000 {
		bysort incorp_no: gen ryear_`y' = runiformint(1996,2010)
		bysort incorp_no: replace ryear_`y' = ryear_`y'[1]
		bysort incorp_no: gen treatment_`y' = runiformint(0,1)
		bysort incorp_no: replace treatment_`y' = treatment_`y'[1]
		gen cow_`y' =0
		replace cow_`y' =1 if treatment_`y'==1 & fyear >= ryear_`y'
		gen cow_`y'_rdassets=cow_`y'*rdassets
	
		quietly reghdfe ln_q rdassets cow_`y' cow_`y'_rdassets d_rd, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		estimate store m`y'
		drop ryear_`y' treatment_`y' cow_`y' cow_`y'_rdassets  
		}

		estout m* using "rd_loop1.csv", cells(b) replace
		

		
		
	clear all
	global path "C:\cow"
	global out "$path\out"
	use "$path\innotest.dta"
	set more off

	*Convert strings to numerics
	destring gvkey, gen(firm)	
	encode state, gen(lstateFE)
	encode incorp, gen(istateFE)
	encode port, gen(ffFE)

	*Set timeframe 
	drop if fyear < 1996 | fyear > 2010

	*Drop if missing industry
	rename sic sic4
	gen sic3 = floor(sic4/10)
	gen sic2 = floor(sic4/100)
	gen sic1 = floor(sic4/1000)
	drop if missing(sic4)
	*Drop utilities
	drop if sic4 >= 4900 & sic4 <= 4999
	*Drop financials
	drop if sic4 >= 6000 & sic4 <= 6999
	*Drop public administration/non-classifiable firms
	drop if sic4 >= 9000 & sic4 <= 9990


	*Drop if missing headquarter state
	drop if missing(state)
	*Drop if missing state of incorporation
	drop if missing(incorp)
	*Drop if not incorporated in U.S. state
	drop if inlist(incorp, "AS", "INTL", "PR", "TT", "VI")

	gen ln_mveq_rd=log(1+mveq_rd)
	gen rdassets = rd_rd/at_rd
	gen rdsales = rd/sales
	gen capxassets = capx/assets
	gen roa=oibdp_rd/at_rd
	drop if mveq_rd<0

	gen d_tsm=0
	replace d_tsm=1 if missing(tsm)
	gen d_fnpats=0
	replace d_fnpats=1 if missing(fnpats)
	
	*Generate variables
	local inno "tcw tsm fnpats  "
	foreach v of local inno {
		replace `v'=0 if missing(`v')
		gen ln_`v' = log(1+`v')
	}
	local varlist "tcw tsm fnpats  "
	foreach v of local varlist {
		gen `v'assets = `v'/assets
	}

	drop if missing(mveq_rd, bm, rdassets, tsmassets, fnpatsassets )
	winsor2 rdassets , replace cuts(1 99) trim
	gen ln_q=log(q)
	

	* Loop 1000;

		forval y= 1/1000 {
		bysort incorp_no: gen ryear_`y' = runiformint(1996,2010)
		bysort incorp_no: replace ryear_`y' = ryear_`y'[1]
		bysort incorp_no: gen treatment_`y' = runiformint(0,1)
		bysort incorp_no: replace treatment_`y' = treatment_`y'[1]
		gen cow_`y' =0
		replace cow_`y' =1 if treatment_`y'==1 & fyear >= ryear_`y'
		gen cow_`y'_tsmassets=cow_`y'*tsmassets
	
		quietly reghdfe ln_q tsmassets cow_`y' cow_`y'_tsmassets d_tsm, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		estimate store m`y'
		drop ryear_`y' treatment_`y' cow_`y'  cow_`y'_tsmassets 
		}

		estout m* using "patvalue_loop1.csv", cells(b) replace

		
		
	clear all
	global path "C:\cow"
	global out "$path\out"
	use "$path\innotest.dta"
	set more off

	*Convert strings to numerics
	destring gvkey, gen(firm)	
	encode state, gen(lstateFE)
	encode incorp, gen(istateFE)
	encode port, gen(ffFE)

	*Set timeframe 
	drop if fyear < 1996 | fyear > 2010

	*Drop if missing industry
	rename sic sic4
	gen sic3 = floor(sic4/10)
	gen sic2 = floor(sic4/100)
	gen sic1 = floor(sic4/1000)
	drop if missing(sic4)
	*Drop utilities
	drop if sic4 >= 4900 & sic4 <= 4999
	*Drop financials
	drop if sic4 >= 6000 & sic4 <= 6999
	*Drop public administration/non-classifiable firms
	drop if sic4 >= 9000 & sic4 <= 9990


	*Drop if missing headquarter state
	drop if missing(state)
	*Drop if missing state of incorporation
	drop if missing(incorp)
	*Drop if not incorporated in U.S. state
	drop if inlist(incorp, "AS", "INTL", "PR", "TT", "VI")

	gen ln_mveq_rd=log(1+mveq_rd)
	gen rdassets = rd_rd/at_rd
	gen rdsales = rd/sales
	gen capxassets = capx/assets
	gen roa=oibdp_rd/at_rd
	drop if mveq_rd<0

	gen d_tsm=0
	replace d_tsm=1 if missing(tsm)
	gen d_fnpats=0
	replace d_fnpats=1 if missing(fnpats)
	
	*Generate variables
	local inno "tcw tsm fnpats  "
	foreach v of local inno {
		replace `v'=0 if missing(`v')
		gen ln_`v' = log(1+`v')
	}
	local varlist "tcw tsm fnpats  "
	foreach v of local varlist {
		gen `v'assets = `v'/assets
	}

	drop if missing(mveq_rd, bm, rdassets, tsmassets, fnpatsassets )
	winsor2 rdassets , replace cuts(1 99) trim
	gen ln_q=log(q)
	

	* Loop 1000;

		forval y= 1/1000 {
		bysort incorp_no: gen ryear_`y' = runiformint(1996,2010)
		bysort incorp_no: replace ryear_`y' = ryear_`y'[1]
		bysort incorp_no: gen treatment_`y' = runiformint(0,1)
		bysort incorp_no: replace treatment_`y' = treatment_`y'[1]
		gen cow_`y' =0
		replace cow_`y' =1 if treatment_`y'==1 & fyear >= ryear_`y'
		gen cow_`y'_fnpatsassets=cow_`y'*fnpatsassets
	
		quietly reghdfe ln_q fnpatsassets cow_`y' cow_`y'_fnpatsassets d_fnpats, absorb(firm lstate#fyear sic3#fyear) vce(cluster istate)
		estimate store m`y'
		drop ryear_`y' treatment_`y' cow_`y'  cow_`y'_fnpatsassets 
		}

		estout m* using "patcount_loop1.csv", cells(b) replace


		
******************************************************
* Figure 1 COW and innovation - Stacked cohort graph *
******************************************************
	clear all
	import excel using "C:\cow\do\leadandfigures.xlsx", firstrow

	sort indexyear
	by indexyear: gen rd_ub = rd + 1.963*rd_se
	by indexyear: gen rd_lb = rd - 1.963*rd_se
	by indexyear: gen patvalue_ub = patvalue + 1.963*patvalue_se
	by indexyear: gen patvalue_lb = patvalue - 1.963*patvalue_se
	by indexyear: gen patcount_ub = patcount + 1.963*patcount_se
	by indexyear: gen patcount_lb = patcount - 1.963*patcount_se

	
	twoway (rarea rd_ub rd_lb indexyear, sort cmissing(n)) (line rd indexyear, sort cmissing(n)), xtitle (indexyear)
	twoway (rarea patvalue_ub patvalue_lb indexyear, sort cmissing(n)) (line patvalue indexyear, sort cmissing(n)), xtitle (indexyear)	
	twoway (rarea patcount_ub patcount_lb indexyear, sort cmissing(n)) (line patcount indexyear, sort cmissing(n)), xtitle (indexyear)

	
	
	
	
